<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.0</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="/static/css/font.css">
    <link rel="stylesheet" href="/static/css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript" src="/static/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/static/js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <!-- 增加 sweetalert -->
    <link rel="stylesheet" href="/static/sweetalert/sweetalert.css">
    <script src="/static/sweetalert/ions_alert.js"></script>
    <script src="/static/sweetalert/sweetalert.min.js"></script>

    <!-- 引入文本编辑器 css -->
    <link rel="stylesheet" href=" /static/froala_editor/css/froala_editor.css">
    <link rel="stylesheet" href="/static/froala_editor/css/froala_style.css">
    <link rel="stylesheet" href="/static/froala_editor/css/plugins/code_view.css">
    <link rel="stylesheet" href="/static/froala_editor/css/plugins/image_manager.css">
    <link rel="stylesheet" href="/static/froala_editor/css/plugins/image.css">
    <link rel="stylesheet" href="/static/froala_editor/css/plugins/table.css">
    <link rel="stylesheet" href="/static/froala_editor/css/plugins/video.css">

</head>
<body>
<div class="x-body">
    <form class="layui-form">
        <div class="layui-form-item">
            <label for="L_email" class="layui-form-label">
                <span class="x-red">*</span>新闻标题
            </label>
            <div class="layui-input-inline">
                <input type="text" id="title" name="title"  lay-verify="required"
                       autocomplete="off" class="layui-input" value="{{ .news_data.Title }}">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="L_username" class="layui-form-label">
                <span class="x-red">*</span>新闻内容
            </label>
            <div class="layui-input-inline">
                <!-- 定义编辑器 -->
                <textarea  id="content" cols="30" rows="10">{{ .news_data.Content }}</textarea>
            </div>
        </div>
        <!-- 增加栏目 -->
        <div class="layui-form-item">
            <label for="L_username" class="layui-form-label">
                <span class="x-red">*</span>栏目
            </label>
            <div class="layui-input-inline">
                <!--增加数据库查询前端显示-->
                <select id="category">
                    <!-- 默认显示 -->
                    <option value="{{ .news_data.Category.Id }}">{{ .news_data.Category.Name }}</option>
                    <!-- 下拉菜单显示所有（排除当前） -->
                    {{ range .categories }}
                    <option value="{{ .Id }}">{{ .Name }}</option>
                    {{ end }}
                </select>
            </div>
        </div>

        <!--启用停用-->
        <div class="layui-form-item">
            <label for="L_repass" class="layui-form-label">
                <span class="x-red">*</span>是否启用
            </label>
            <div class="layui-input-inline">
                <!-- 增加 -->
                {{ if eq .news_data.IsActive 1 }}
                <input type="radio" name="is_active" autocomplete="off" class="layui-input" value="1" title="启用" checked>

                <input type="radio" name="is_active" required="" lay-verify="repass"
                       autocomplete="off" class="layui-input" value="0" title="停用">
                {{else}}
                <input type="radio" name="is_active" autocomplete="off" class="layui-input" value="1" title="启用">

                <input type="radio" name="is_active" required="" lay-verify="repass"
                       autocomplete="off" class="layui-input" value="0" title="停用" checked>
                {{end}}
            </div>
        </div>
        <!---->
        <div class="layui-form-item">
            <input type="hidden" id="news_id" value="{{ .news_data.Id }}">
            <label for="L_repass" class="layui-form-label">
            </label>
            <button  class="layui-btn" type="submit">
                提交
            </button>
        </div>
    </form>
</div>

<!-- 引入文本编辑器 js -->
<script type="text/javascript" src="/static/froala_editor/js/froala_editor.min.js"></script>
<script type="text/javascript" src="/static/froala_editor/js/plugins/align.min.js"></script>
<script type="text/javascript" src="/static/froala_editor/js/plugins/code_beautifier.min.js"></script>
<script type="text/javascript" src="/static/froala_editor/js/plugins/code_view.min.js"></script>
<script type="text/javascript" src="/static/froala_editor/js/plugins/draggable.min.js"></script>
<script type="text/javascript" src="/static/froala_editor/js/plugins/image.min.js"></script>
<script type="text/javascript" src="/static/froala_editor/js/plugins/image_manager.min.js"></script>
<script type="text/javascript" src="/static/froala_editor/js/plugins/link.min.js"></script>
<script type="text/javascript" src="/static/froala_editor/js/plugins/lists.min.js"></script>
<script type="text/javascript" src="/static/froala_editor/js/plugins/paragraph_format.min.js"></script>
<script type="text/javascript" src="/static/froala_editor/js/plugins/paragraph_style.min.js"></script>
<script type="text/javascript" src="/static/froala_editor/js/plugins/table.min.js"></script>
<script type="text/javascript" src="/static/froala_editor/js/plugins/video.min.js"></script>

<script>
    /*定义ajax*/
    $(function  () {
        /* 文本编辑器 ⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️
        file:///Users/yangyi-for-mac/%E7%99%BE%E5%BA%A6%E7%BD%91%E7%9B%98%E4%B8%8B%E8%BD%BD%E8%B5%84%E6%96%99/01115%20-%20Go,Golang,Beego%E5%BE%AE%E6%9C%8D%E5%8A%A1%E5%9F%BA%E7%A1%80%E5%AE%9E%E6%88%98/%E8%B5%84%E6%96%99%E4%BB%A3%E7%A0%81/%E8%AF%BE%E4%BB%B6/%E7%9F%A5%E4%BA%86%E4%BC%A0%E8%AF%BEBeego2%E8%AF%BE%E4%BB%B6/chapter11-%E5%AE%9E%E6%88%98/di-si-jie-ff1a-fu-wen-ben-bian-ji-qi.html
        */

        var editor = new FroalaEditor("#content", {
            width : 300,   // 尺寸
            height : 300,
            placeholderText:"请输入新闻内容",  // 背影描述
            charCounterCount : true,  // 计数器（未生效）
            language : "zh-cn",
            imageUploadMethod: "POST",
            imageUploadURL : "{{ urlfor `NewsController.UpLoadImg` }}",
            toolbarButtons: ['bold', 'italic', 'underline', 'paragraphFormat', 'align','color','fontSize','insertImage','insertTable','undo', 'redo'],
            events : {
                // 初始化一个函数
                initialized : function (){
                    // 获取输入内容对象
                    const editor = this;
                    // 监听当前 提交，触发function打印
                    this.el.closest('form').addEventListener('submit', function (e) {
                        var content = editor.$oel.val();
                        // 数据传到后端
                        var title = document.getElementById("title").value;
                        var news_id = document.getElementById("news_id").value;
                        var category_id = document.getElementById("category").value;
                        var is_active = $("input[name='is_active']:checked").val();
                        /*console.log(content);
                        console.log(title);
                        console.log(category);
                        console.log(is_active);*/

                        if (category_id == 0 ) {
                            alert("请选择栏目");
                            return
                        }
                        if (title.length == 0) {
                            alert("请选择栏目");
                            return
                        }
                        // 增加ajax提交
                        /* ajax调用 */
                        $.ajax({
                            url : "{{ urlfor `NewsController.DoEdit` }}",
                            type : "POST",
                            data : {
                                "content" : content,
                                "title" : title,
                                "category_id" : category_id,
                                "is_active" : is_active,
                                "news_id":news_id
                            },
                            success:function (data){
                                /* 添加后端显示数据 */
                                var code = data["code"];
                                if (code == 200) {
                                    alert(data["msg"]);
                                    /* 获得frame索引 */
                                    var index = parent.layer.getFrameIndex(window.name);
                                    /* 关闭当前frame,关闭弹出小窗口 */
                                    parent.layer.close(index);
                                    window.location.href = "{{ urlfor `NewsController.Get` }}"
                                    /*                                      ions_alert.alertSuccessToast(data["msg"]);
                                                                            setTimeout(reload_list,2000);*/
                                } else  {
                                    alert(data["msg"]);
                                    ions_alert.alertErrorToast(data["msg"]);
                                }
                            },
                            fail:function (data) {
                                alert(data);
                            }
                        });
                        /* 重定向 */
                        function reload_list () {
                            /* 获得frame索引 */
                            var index = parent.layer.getFrameIndex(window.name);
                            /* 关闭当前frame,关闭弹出小窗口 */
                            parent.layer.close(index);
                            window.location.href = "{{ urlfor `NewsController.Get` }}"
                        }
                    })
                }
            }
        });
        /* 文本编辑器 ⬆️⬆️⬆️⬆️⬆️⬆️⬆️⬆️⬆️⬆️⬆️⬆️⬆️ */
    })
</script>
</body>
</html>